package com.xyy.ec.qualification.utils;

import com.alibaba.fastjson.JSON;
import com.xyy.ec.qualification.enums.DBTypeEnum;
import com.xyy.ec.qualification.pojo.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author suolong
 */
public class ConnectDB {
    private static final Logger logger = LoggerFactory.getLogger(ConnectDB.class);


    public static boolean testConnection(DataSource dataSource){
        Connection connection = null;
        try {
            connection = getConnection(dataSource);
            if (null != connection){
                return true;
            }
        } catch (SQLException e) {
            logger.error("testConnection_dataSource={}", JSON.toJSONString(dataSource), e);
        } finally {
            if (null != connection){
                try {
                    connection.close();
                } catch (Throwable t) {
                    logger.error("testConnection_dataSource={}", JSON.toJSONString(dataSource), t);
                }
            }
        }
        return false;
    }
    public static Connection getConnection(DataSource dataSource) throws SQLException {
        if (DBTypeEnum.MYSQL.getDbType().equalsIgnoreCase(dataSource.getDbType())){
            classForDriver(DBTypeEnum.MYSQL);
        }else if (DBTypeEnum.ORACLE.getDbType().equalsIgnoreCase(dataSource.getDbType())){
            classForDriver(DBTypeEnum.ORACLE);
        }else if (DBTypeEnum.SQL_SERVER.getDbType().equals(dataSource.getDbType())){
            classForDriver(DBTypeEnum.SQL_SERVER);
        }else if (DBTypeEnum.SQL_SERVER_V2000.getDbType().equals(dataSource.getDbType())){
            classForDriver(DBTypeEnum.SQL_SERVER_V2000);
        }else if (DBTypeEnum.ORACLE_9I.getDbType().equalsIgnoreCase(dataSource.getDbType())){
            classForDriver(DBTypeEnum.ORACLE_9I);
        }else {
            return null;
        }
        return DriverManager.getConnection(dataSource.getUrl(), dataSource.getUserName(), dataSource.getPassword());
    }

    private static Connection getMySqlConn(DataSource dataSource) throws SQLException {
        return DriverManager.getConnection(dataSource.getUrl(), dataSource.getUserName(), dataSource.getPassword());
    }

    private static void classForDriver(DBTypeEnum dbTypeEnum){
        try {
            Class.forName(dbTypeEnum.getDriver());
        } catch (ClassNotFoundException e) {
            logger.info("classForDriver_dbTypeEnum={}", JSON.toJSONString(dbTypeEnum), e);
        }
    }

    private static Connection getOracleConn(DataSource dataSource) throws SQLException {
        Connection conn = null;
        conn = DriverManager.getConnection(dataSource.getUrl(), dataSource.getUserName(), dataSource.getPassword());

        return conn;
    }

    private static Connection getSqlServerConn(DataSource dataSource) throws SQLException {
        return DriverManager.getConnection(dataSource.getUrl(), dataSource.getUserName(), dataSource.getPassword());

    }

}
